package com.glory.good_dark.entity.response;

import android.app.Activity;
import android.content.Context;
import android.widget.Toast;

import androidx.fragment.app.FragmentManager;

import com.glory.good_dark.application.App;
import com.pda.platform.ui.ui_pdaplatform.dialog.FreeUI_GeneralFragmentDialog;
import com.pda.platform.ui.ui_pdaplatform.utils_public.FreeApi_DialogUtils;
import com.pda.platform.ui.ui_pdaplatform.utils_public.FreeApi_ToastUtils;

import java.io.Serializable;

/**
 * @author anyang
 * @date 2017/9/12
 * @desc
 */

public class BaseEntity<T> implements Serializable {

    private ResponseEntity<T> data;

    private transient FreeUI_GeneralFragmentDialog tipDialog;

    public ResponseEntity<T> getData() {
        return data;
    }

    public void setData(ResponseEntity<T> message) {
        this.data = message;
    }

    //获取到对象后，必须要判断是否获取成功
    public boolean isSuccess(Context context,FragmentManager fragmentManager) {
        if (data != null) {
            if (data.getReturn().getRETURNCODE() == 0) { //新增测试用
                  //去除boby为空的判断
//                if (Message.getBody() != null) {
//                        return true;
//                    } else {
//                        FreeApi_ToastUtils.showFreeToast("返回Body数据为空，请检查后台数据", context, false, Toast.LENGTH_SHORT);
//                        return false;
//                    }
                return true;
            } else {
                    showFailDialog(data.getReturn().getRETURNMESSAGE(),fragmentManager);
                    return false;
            }
        } else {
                FreeApi_ToastUtils.showFreeToast("返回data数据为空，请检查后台数据", context, false, Toast.LENGTH_LONG);
                return false;
        }
    }

    public boolean isSuccess(Activity context,FragmentManager fragmentManager,boolean isFinish) {
        if (data != null) {
            if (data.getReturn().getRETURNCODE() == 0) { //新增测试用

                return true;
            } else {
                if (isFinish){
                    showFailDialog(context,data.getReturn().getRETURNMESSAGE(),fragmentManager);
                }  else {
                    showFailDialog(data.getReturn().getRETURNMESSAGE(),fragmentManager);
                }
                return false;
            }
        } else {
            FreeApi_ToastUtils.showFreeToast("返回data数据为空，请检查后台数据", context, false, Toast.LENGTH_LONG);
            return false;
        }
    }

    private void showFailDialog(String message, FragmentManager fragmentManager)
    {
        tipDialog = FreeApi_DialogUtils.getConfirmDialog(App.app, "确定", message, () -> {

        });
        tipDialog.show(fragmentManager,"DIALOG");
    }

    private void showFailDialog(Activity context, String message, FragmentManager fragmentManager)
    {
        tipDialog = FreeApi_DialogUtils.getConfirmDialog(App.app, "确定", message, () -> {
            context.finish();
        });
        tipDialog.show(fragmentManager,"DIALOG");
    }

}
